import {
    fetchApplicantList,
    fetchCommonWorkflowList,
    fetchApproverList
} from '@/services/flowManage/rank';

export default {
    namespace: 'rank',
    state: {
        applicant: {
            list: [],
            pagination: {
                current: 1,
                pageSize: 10,
                total: 0
            }
        },
        commonWorkflow: {
            list: [],
            pagination: {
                current: 1,
                pageSize: 10,
                total: 0
            }
        },
        approver: {
            List: [],
            pagination: {
                current: 1,
                pageSize: 10,
                total: 0
            }
        },
        extra: {}
    },
    effects: {
        *fetchList({ payload, fetchType }, { call, put }) {
            const apiMap = {
                applicant: fetchApplicantList,
                commonWorkflow: fetchCommonWorkflowList,
                approver: fetchApproverList
            };

            const result = yield call(apiMap[fetchType], payload);

            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        [fetchType]: {
                            list: result.data.rows,
                            pagination: {
                                pageSize: result.data.pageSize,
                                current: result.data.pageNo,
                                total: result.data.total
                            }
                        }
                    }
                });
            }
        }
    },
    reducers: {
        save(state, { payload }) {
            return {
                ...state,
                ...payload
            };
        }
    }
};
